
#include <stdio.h>
#include <string.h>
#include "xdisk.h"
#include "xfat.h"

extern xdisk_driver_t vdisk_driver;
xdisk_t vdisk;
xdisk_part_t part_root;
xfat_t xfat;

static u8_t write_buffer[1024];
static u8_t read_buffer[1024];

xfat_err_t vdisk_test(void)
{
    xfat_err_t err = FS_ERR_EOK;

    err = xdisk_init(&vdisk, "vdisk", &vdisk_driver, "disk_test.img");
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test init error %d\r\n", err);
    }

    for ( int i = 0; i < sizeof(write_buffer); i++)
    {
        write_buffer[i] = i;
    }
    
    err = xdisk_write(&vdisk, write_buffer, 0 , 2);
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test write error %d\r\n", err);
        return err;
    }
    
    err = xdisk_read(&vdisk, read_buffer, 0 , 2);
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test read error %d\r\n", err);
        return err;
    }

    if (memcmp(read_buffer, write_buffer, sizeof(read_buffer)) != 0)
    {
        printf("vdisk test compare error %d\r\n", err);
        return err;
    }
    
    printf("vdisk test ok\r\n");
    return err;
}

int main (void)
{
    xfat_err_t err = FS_ERR_EOK;

    printf("test start\r\n");

    err = vdisk_test();
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test error %d\r\n", err);
    }

    err = xdisk_init(&vdisk, "vdisk", &vdisk_driver, "disk.img");
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test init error %d\r\n", err);
    }

    part_root.next = part_root.prev = &part_root;
    err = xdisk_parse(&vdisk, &part_root);
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test xdisk parse error %d\r\n", err);
    }

    err = xfat_prase(&xfat, part_root.next->next);
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test xfat prase error %d\r\n", err);
    }

    err = list_xfat_root_dir(&xfat);
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test xfat list root dir error %d\r\n", err);
    }

    err = list_xfat_file_content(&xfat, 4565);
    if (err != FS_ERR_EOK)
    {
        printf("vdisk test xfat list read file error %d\r\n", err);
    }

    printf("test end\r\n");
    return 0;
}